#include "PositionEvent.h"
#include "InterpolationProcessor.h"
#include "ConvertValueToString.h"

PositionEvent::PositionEvent(Ogre::Vector3 past,Ogre::Vector3 current){
	this->_past_value = ConvertValueToString::Vector3toString(past);
	this->_current_value = ConvertValueToString::Vector3toString(current);
	this->_name = "Position_Change";
	this->_pos_variation = Ogre::StringConverter::parseVector3(this->_past_value);
}

void PositionEvent::writePastValueToVariation(){
	this->_variation = this->_past_value;
	this->_pos_variation = Ogre::StringConverter::parseVector3(this->_past_value);
}

void PositionEvent::generateInterpolation(){
	InterpolationProcessor ipp;
	Ogre::Vector3 pastvalue = Ogre::StringConverter::parseVector3(this->_past_value);
	Ogre::Vector3 currentvalue = Ogre::StringConverter::parseVector3(this->_current_value);
	Ogre::Vector3 ipvalue = ipp.excution(pastvalue,currentvalue);
	this->_ipValue = ConvertValueToString::Vector3toString(ipvalue);
}

bool PositionEvent::checkInterpolationValueIsZero(){
	Ogre::Vector3 ipvalue = Ogre::StringConverter::parseVector3(this->_ipValue);
	if(ipvalue == Ogre::Vector3::ZERO){
		return true;
	}
	return false;
}

bool PositionEvent::checkBounding(){
	Ogre::Vector3 pos_ipvalue = Ogre::StringConverter::parseVector3(this->_ipValue);
	if(pos_ipvalue != Ogre::Vector3::ZERO){
		this->_pos_variation+= pos_ipvalue;
		this->_variation = ConvertValueToString::Vector3toString(this->_pos_variation);
		Ogre::Vector3 pos_current_value = Ogre::StringConverter::parseVector3(this->_current_value);
		if(isOverflow(this->_pos_variation.x,pos_current_value.x,pos_ipvalue.x) ||
			isOverflow(this->_pos_variation.y,pos_current_value.y,pos_ipvalue.y) ||
			isOverflow(this->_pos_variation.z,pos_current_value.z,pos_ipvalue.z)){
				this->_pos_variation = Ogre::StringConverter::parseVector3(this->_past_value);
				this->_variation = ConvertValueToString::Vector3toString(this->_pos_variation);
				return false;
		}
	}
	return true;
}

void PositionEvent::recover(){
	writePastValueToVariation();
	this->_ipValue = "0.0 0.0 0.0";
	this->_pos_variation = Ogre::StringConverter::parseVector3(this->_past_value);
}